package net.sf.rose.file.excel.poi.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.util.BeanWrapper;

/**
 * @author fengjian
 * @since 2016/05/08
 */
public class CellModel {

	/** 单元格所在行 */
	private int row = 0;

	/** 单元格所在列 */
	private int col = 0;

	/** 模板文本内容 */
	private String content = null;

	/** 键名 */
	private String key = null;

	/** 主属性名 */
	private String mainName = null;

	/** 子属性名 */
	private String subName = null;

	private BeanWrapper bw = new BeanWrapper();

	public CellModel() {
		super();
	}

	public CellModel(int row, int col) {
		super();
		this.row = row;
		this.col = col;
	}

	/**
	 * 往右复制一个单元格
	 * $[list.workNo]
	 * 
	 * @return
	 */
	public CellModel copy(String key) {
		CellModel cell = new CellModel();
		cell.row = this.row;
		cell.col = this.col + 1;
		int index = this.content.indexOf('.') + 1;
		String str = this.content.substring(0, index) + key + ']';
		cell.setContent(str);
		return cell;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getCol() {
		return col;
	}

	public void setCol(int col) {
		this.col = col;
	}

	public String getMainName() {
		return mainName;
	}

	public String getSubName() {
		return subName;
	}

	public void setSubName(String subName) {
		this.subName = subName;
	}

	public void setContent(String content) {
		this.content = content;
		int i = content.indexOf('$');
		int j = 0, k = 0;
		if (content.charAt(i + 1) == '{') {
			j = content.indexOf('}', i);
			key = content.substring(i, j + 1);
			mainName = key.substring(2, key.length() - 1);
		} else if (content.charAt(i + 1) == '[') {
			j = content.indexOf(']', i);
			key = content.substring(i, j + 1);
			String str = key.substring(2, key.length() - 1);
			k = str.indexOf('.');
			mainName = str.substring(0, k);
			subName = str.substring(k + 1, str.length());
		}
	}

	public PageBean getCellValue(HSSFCell cell, Object bean) {
		Object val = null;
		int cellType = cell.getCellType();
		switch (cellType) {
			case HSSFCell.CELL_TYPE_STRING:
				val = cell.getStringCellValue();
				break;
			case HSSFCell.CELL_TYPE_NUMERIC:
				if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
					val = cell.getDateCellValue();
				} else {
					val = cell.getNumericCellValue();
				}
				break;
			case HSSFCell.CELL_TYPE_BOOLEAN:
				val = String.valueOf(cell.getBooleanCellValue());
				break;
			case HSSFCell.CELL_TYPE_BLANK:
				val = cell.getStringCellValue();
				break;
			case HSSFCell.CELL_TYPE_ERROR:
				val = "错误";
				break;
			case HSSFCell.CELL_TYPE_FORMULA:
				String formula = cell.getCellFormula();
				System.out.println(formula);
				break;
			default:
				val = cell.getRichStringCellValue();
		}
		return this.setData(bean, val);
	}

	@SuppressWarnings("unchecked")
	private PageBean setData(Object bean, Object val) {
		// 如果单元格的值为空，或者为-，则不作解析
		if (val == null) {
			return new PageBean();
		}
		String str = val.toString().trim();
		if (str.equals("") || str.equals("-")) {
			return new PageBean();
		}
		String name = subName != null ? subName : mainName;
		if (bean instanceof Map) {
			((Map<String, Object>) bean).put(name, val);
			return new PageBean();
		} else {
			bw.setWrappedInstance(bean);
			return bw.set(name, val);
		}
	}

	/**
	 * 功能：设置单元格的值，如果是列表设置一列
	 * 
	 * @param cell 需要设置的单元格
	 * @param bean 源数据
	 */
	@SuppressWarnings("unchecked")
	public void setCellValue(HSSFCell cell, Object bean) {
		Object val = null;
		String name = subName != null ? subName : mainName;
		if (bean instanceof Map) {
			Map<String, Object> map = (Map<String, Object>) bean;
			val = map.get(name);
		} else {
			bw.setWrappedInstance(bean);
			val = bw.get(name);
		}
		if (val != null) {
			setValue(cell, val);
		} else {
			cell.setCellValue("");
		}
	}

	/**
	 * 功能：设置单元格的值，如果是列表设置一列
	 * 
	 * @param cell 需要设置的单元格
	 * @param map  源数据
	 */
	private void setValue(HSSFCell cell, Object val) {
		// 如果单元格内包括变量以外的常量
		if (!this.content.equals(this.key)) {
			String str = StringUtils.replace(this.content, this.key, val.toString());
			cell.setCellValue(str);
		} else {
			if (val instanceof String) {
				cell.setCellValue(val.toString());
			} else if (val instanceof Integer) {
				cell.setCellValue((int) val);
			} else if (val instanceof Double) {
				cell.setCellValue((double) val);
			} else if (val instanceof Date) {
				cell.setCellValue((Date) val);
			} else if (val instanceof BigDecimal) {
				double d = ((BigDecimal) val).doubleValue();
				cell.setCellValue(d);
			} else {
				cell.setCellValue(val.toString());
			}
		}
	}

	public PageBean getCellValue(XSSFCell cell, Object bean) {
		Object val = null;
		int cellType = cell.getCellType();
		switch (cellType) {
			case XSSFCell.CELL_TYPE_STRING:
				val = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_NUMERIC:
				if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
					val = cell.getDateCellValue();
				} else {
					val = cell.getNumericCellValue();
				}
				break;
			case XSSFCell.CELL_TYPE_BOOLEAN:
				val = String.valueOf(cell.getBooleanCellValue());
				break;
			case XSSFCell.CELL_TYPE_BLANK:
				val = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_ERROR:
				val = "错误";
				break;
			case XSSFCell.CELL_TYPE_FORMULA:
				String formula = cell.getCellFormula();
				System.out.println(formula);
				break;
			default:
				val = cell.getRichStringCellValue();
		}
		return this.setData(bean, val);
	}

	/**
	 * 功能：设置单元格的值，如果是列表设置一列
	 * 
	 * @param cell 需要设置的单元格
	 * @param bean 源数据
	 */
	@SuppressWarnings("unchecked")
	public void setCellValue(XSSFCell cell, Object bean) {
		Object val = null;
		String name = subName != null ? subName : mainName;
		if (bean instanceof Map) {
			Map<String, Object> map = (Map<String, Object>) bean;
			val = map.get(name);
		} else {
			bw.setWrappedInstance(bean);
			val = bw.get(name);
		}
		if (val != null) {
			setValue(cell, val);
		} else {
			cell.setCellValue("");
		}
	}

	/**
	 * 功能：设置单元格的值，如果是列表设置一列
	 * 
	 * @param cell 需要设置的单元格
	 * @param val  源数据
	 */
	private void setValue(XSSFCell cell, Object val) {
		// 如果单元格内包括变量以外的常量
		if (!this.content.equals(this.key)) {
			String str = StringUtils.replace(this.content, this.key, val.toString());
			cell.setCellValue(str);
		} else {
			if (val instanceof String) {
				cell.setCellValue(val.toString());
			} else if (val instanceof Integer) {
				cell.setCellValue((int) val);
			} else if (val instanceof Double) {
				cell.setCellValue((double) val);
			} else if (val instanceof Date) {
				cell.setCellValue((Date) val);
			} else if (val instanceof BigDecimal) {
				double d = ((BigDecimal) val).doubleValue();
				cell.setCellValue(d);
			} else {
				cell.setCellValue(val.toString());
			}
		}
	}
}
